import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/utils/search_module.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../../components/bottom_enter_page.dart';
import 'create_leave_controller.dart';

class CreateLeaveView extends GetView{


  CreateLeaveController controller = Get.put(CreateLeaveController());

  bool submit = false;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text(controller.title),
      ),
      body: GetBuilder<CreateLeaveController>(builder: (_){
        return GestureDetector(
          onTap: (){
            SearchModule.closeKeyboard(context);
          },
          onPanDown: (_) {
            /// 列表滑动响应
            SearchModule.closeKeyboard(context);
          },

          child: Column(
            children: [
              Expanded(child: ListView(
                children: [
                  BrnInputText(
                    maxHeight: 200,
                    minHeight: 30,
                    minLines: 1,
                    maxLength: 50,
                    autoFocus: false,
                    bgColor: Colors.white,
                    textInputAction: TextInputAction.newline,
                    maxHintLines: 20,
                    textEditingController: controller.editingController,
                    hint: '请假的原因',
                    padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                    onTextChange: (text) {},
                    onSubmit: (text) {
                      print(text);
                    },
                  ),
                  submit==false?const SizedBox():
                  controller.editingController.text==''?buildFieldError('请输入请假原因'):SizedBox(),


                  const SizedBox(
                    height: 1,
                  ),
                  GestureDetector(

                      onTap: () async {
                        // controller.taoClickUser();
                        controller.tapClickLeaveType();
                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                        color: Colors.white,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text("请假类型"),
                            Container(
                                height: 40,
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceAround,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Text('${controller.holidayStatusName}'),
                                    const Icon(
                                      Icons.arrow_forward_ios,
                                      size: 15,
                                    )
                                  ],
                                )),
                          ],
                        ),
                      )),
                  submit==false?const SizedBox():
                  controller.holidayStatusId==null?buildFieldError('请选择请假类型'):SizedBox(),
                  const SizedBox(
                    height: 1,
                  ),
                  GestureDetector(
                      onTap: () {
                        // controller.tapClickLeaveUnit(['天', '半天', '小时']);
                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                        color: Colors.white,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text("单位"),
                            Container(
                                height: 40,
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceAround,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Text("${controller.requestUnitName}"),
                                  ],
                                )),
                          ],
                        ),
                      )),


                  const SizedBox(
                    height: 1,
                  ),
                  GestureDetector(

                      onTap: () async {
                        if(controller.requestUnitName=='小时'){
                          controller.tapClickStartTimeHour();
                        }else{
                          controller.tapClickStartTime();
                        }
                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                        color: Colors.white,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text("开始时间"),
                            Container(
                                height: 40,
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceAround,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Text('${controller.startTime}'),
                                    const Icon(
                                      Icons.arrow_forward_ios,
                                      size: 15,
                                    )
                                  ],
                                )),
                          ],
                        ),
                      )),
                  controller.startTime=='请选择开始时间'?buildFieldError('请选择开始时间'):SizedBox(),
                  const SizedBox(
                    height: 1,
                  ),
                  GestureDetector(

                      onTap: () async {

                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                        color: Colors.white,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text("结束时间"),
                            Container(
                                height: 40,
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceAround,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Text('${controller.endTime}'),
                                  ],
                                )),
                          ],
                        ),
                      )),

                  const SizedBox(
                    height: 1,
                  ),
                  GestureDetector(

                      onTap: () async {
                    
                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                        color: Colors.white,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text("时长"),
                           controller.requestUnitName=='小时'?Container(
                             child:   controller.startTime=='请选择开始时间'||controller.endTime=='结束时间'
                                 ?Container():Text('${controller.timeIntervalHour(DateTime.parse(
                                 controller.startTime
                             ),
                                 DateTime.parse(controller.endTime))}分钟'),
                           ):Container(
                                height: 40,
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceAround,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                 controller.startTime=='请选择开始时间'||controller.endTime=='结束时间'
                                     ?Container():Text('${controller.timeInterval(DateTime.parse(
                                      controller.startTime
                                    ),
                                     DateTime.parse(controller.endTime))}天'),
                                  ],
                                )),
                          ],
                        ),
                      )),
                  const SizedBox(
                    height: 1,
                  ),
                  controller.fileName==''?Container():
                  Container(
                    color: Colors.white,
                    width: Get.width-100,
                    height: 60,
                    padding: EdgeInsets.only(left: 15,top: 5,right: 25),
                    child:   Text('附件：${controller.fileName}',maxLines: 2,),
                  ),
                  MaterialButton(onPressed: (){
                    controller.tapClickAccessory();
                  },child: Text('点击添加附件'),),
                ],
              )),
              Container(
                color: Colors.white,
                child: BottomEnterPage(() {
                  submit = true;
                  controller.update();
                  if(controller.editingController.text.isEmpty||controller.holidayStatusId==null||
                  controller.startTime=='请选择开始时间'){

                    return;
                  }

                  controller.requestDataWithPublish();
                }),
              )
            ],
          ),
        );
      }),
    );
  }
  Widget buildFieldError(errorType) {
    return Padding(padding: const EdgeInsets.only(left: 15),child: Row(
      children: [
        // if(errors.contains(errorType))
        Expanded(
          child: Text(
              '$errorType',
              textAlign: TextAlign.left,
              style: const TextStyle(
                  color: Colors.red,
                  fontSize: 12
              )
          ),
        ),
      ],
    ),);
  }

}